package com.weiyin.examination.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.weiyin.examination.R;
import com.weiyin.examination.adapter.SelectTopicAdapter;
import com.weiyin.examination.listener.SelectTopicDialogResultListener;
import com.weiyin.examination.module.CourseAnswerModule;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 选题弹窗
 *
 * @author njf
 */
public class SelectedTopicDialog extends Dialog implements SelectTopicAdapter.OnItemClickInterface {
    private static final String TAG = SelectedTopicDialog.class.getSimpleName();
    private Activity activity;
    private RecyclerView recyclerView;
    private SelectTopicAdapter selectTopicAdapter;
    private int topicNumber;

    private List<CourseAnswerModule> topicNumberList;
    private String[] number = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};
    private boolean[] resultS = {false, false, false, false, false, false, false, false, false, false, false, false};
    private SelectTopicDialogResultListener selectTopicDialogResultListener;

    public void setSelectTopicDialogResultListener(SelectTopicDialogResultListener selectTopicDialogResultListener) {
        this.selectTopicDialogResultListener = selectTopicDialogResultListener;
    }

    public SelectedTopicDialog(@NonNull Activity context) {
        super(context);
        this.activity = context;
    }

    public SelectedTopicDialog(@NonNull Activity context, @StyleRes int themeResId, int position) {
        super(context, themeResId);
        this.activity = context;
        this.topicNumber = position;
    }

    protected SelectedTopicDialog(@NonNull Activity context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.activity = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_topic_dialog_layout);
        LayoutHelper layoutHelper = new LayoutHelper(activity);
        layoutHelper.scaleView(findViewById(R.id.select_dialog_layout));
        LogUtil.i(TAG, " onCreate");
        initData();
        initView();
    }

    private void initData() {
        if (topicNumberList == null) {
            topicNumberList = new ArrayList<>();
        } else {
            topicNumberList.clear();
        }
        for (int i = 0; i < topicNumber; i++) {
            if (i < number.length) {
                CourseAnswerModule courseAnswerModule = new CourseAnswerModule();
                courseAnswerModule.setString(number[i]);
                courseAnswerModule.setResult(resultS[i]);
                topicNumberList.add(courseAnswerModule);
            }
        }
    }

    public void setTopicNumberList(int position, boolean result) {
        if (topicNumberList != null) {
            topicNumberList.get(position).setResult(result);
            topicNumberList.set(position, topicNumberList.get(position));
            if (selectTopicAdapter != null) {
                selectTopicAdapter.notifyDataSetChanged();
            }
        } else {
            resultS[position] = result;
        }
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.select_dialog_recyclerView);
        selectTopicAdapter = new SelectTopicAdapter(activity, R.layout.select_topic_dialog_item_layout, topicNumberList);
        recyclerView.setAdapter(selectTopicAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));

        selectTopicAdapter.setOnItemClickInterface(this);
    }

    @Override
    public void onItemClick(BaseViewHolder baseViewHolder, int position) {
        if (selectTopicDialogResultListener != null) {
            selectTopicDialogResultListener.selectDialogResult(position);
        }
    }
}
